﻿

CREATE PROCEDURE [dbo].[fares_FiltreazaRaportPlati] 
    @filtruPart varchar(8000),
	@filtruDocPlata varchar(8000),
	@data1 datetime,
	@data2 datetime,
	@numaiAvansuri bit,
	@documenteCLB smallint,
	@perioadaFactura bit,
	@data1p datetime,
	@data2p datetime,	
	@CodUnitate smallint
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

  SELECT * FROM [fares_Plati] WHERE 
    (LEN(LTRIM(RTRIM(@filtruDocPlata)))=0 OR (IDTipPlata IN (SELECT fart.value FROM dbo.SplitIntParameters(@filtruDocPlata,',') fart))) AND
    (LEN(LTRIM(RTRIM(@filtruPart)))=0 OR (IDPartener IN (SELECT fart.value FROM dbo.SplitBigIntParameters(@filtruPart,',') fart))) AND
 	CodUnitate = @CodUnitate AND [Data]>=@data1 AND [Data]<=@data2 AND
 	(@numaiAvansuri=0 OR (@numaiAvansuri=1 AND [NrDoc] is NULL)) AND
 	((@documenteCLB=0 AND [TipOperatiune]='I') OR (@documenteCLB=1 AND [TipOperatiune]='O')) AND
 	(@perioadaFactura=0 OR (@perioadaFactura=1 AND [DataDoc]>=@data1p AND [DataDoc]<=@data2p))
	ORDER BY [DataDoc]
END